import './index.less';
import {inject, observer} from 'mobx-react';
import React from 'react';

import {WeaTop, WeaTab, WeaRightMenu} from 'ecCom';
import AdvanceSearch from './AdvanceSearch';
import FlowAnalyseCount from '../report-public/flow-analyse-count/index';
import AnalyseEchart from './AnalyseEchart';
import FlowReportDetail from '../report-public/flow-report-detail/index';
import * as ReportUtil from '../../../util/reportUtil';
import ReportNoRight from '../report-public/report-no-right/index';

@inject('handleRequestAnalyseStore')
@observer
export default class Main extends React.Component {

    constructor(props) {
        super(props);

        const {handleRequestAnalyseStore} = props;
        const {initDatas} = handleRequestAnalyseStore;
        initDatas && initDatas();
    }

    componentWillReceiveProps(nextProps) {
        if (this.props.location.key !== nextProps.location.key) {
            const {clearStatus, initDatas} = this.props.handleRequestAnalyseStore;
            clearStatus();
            initDatas();
        }
    }

    render() {
        const {handleRequestAnalyseStore} = this.props;
        const {loading = false, advanceSearchData = {}, advanceSearchPara = {}, analyseData = {}, modalPara = {}, labels = {}} = handleRequestAnalyseStore;
        const {updateAdvanceSearchPara, showModal, closeModal, updateModalPara} = handleRequestAnalyseStore; //action
        const {reportTitle = ''} = labels;
        const {tabDatas = [], noRight = false, bottomInfo = ''} = advanceSearchData;
        const {countData = [], echartDatas = {}} = analyseData;
        const {tabKey = 1} = advanceSearchPara;
        const echartTitle = labels[`echartTitle${tabKey}`];
        return (
            <div className="wf-handle-request-analyse">
                {noRight ?
                    <ReportNoRight ecId={`${this && this.props && this.props.ecId || ''}_ReportNoRight@czdagp`}/>
                    :
                    <WeaTop ecId={`${this && this.props && this.props.ecId || ''}_WeaTop@qmnbsl`} loading={loading} icon={<i className='icon-coms-workflow'/>} buttons={[]}
                            iconBgcolor='#0079DE' title={reportTitle} showDropIcon={false}>
                        <WeaTab ecId={`${this && this.props && this.props.ecId || ''}_WeaTab@1cwkeu`} datas={tabDatas} keyParam={"key"} selectedKey={tabKey}
                                onChange={this.doChangeTab.bind(this)}/>
                        <AdvanceSearch ecId={`${this && this.props && this.props.ecId || ''}_AdvanceSearch@9owq7x`} advanceSearchData={advanceSearchData} advanceSearchPara={advanceSearchPara}
                                       updateAdvanceSearchPara={updateAdvanceSearchPara} tabKey={tabKey}/>
                        <FlowAnalyseCount ecId={`${this && this.props && this.props.ecId || ''}_FlowAnalyseCount@9yttyi`} datas={countData}/>
                        <AnalyseEchart ecId={`${this && this.props && this.props.ecId || ''}_AnalyseEchart@752vug`} loading={loading} echartTitle={echartTitle} tabKey={tabKey}
                                       eChartDatas={echartDatas} showModal={showModal}/>
                        <FlowReportDetail ecId={`${this && this.props && this.props.ecId || ''}_FlowReportDetail@3wx3rs`} {...modalPara} closeModal={closeModal} updateModalPara={updateModalPara}/>
                        <div className='count-bottom'>{bottomInfo}</div>
                    </WeaTop>
                }
            </div>
        )
    }

    doChangeTab(tabKey) {
        const {handleRequestAnalyseStore} = this.props;
        const {updateAdvanceSearchPara, advanceSearchData, advanceSearchPara = {}} = handleRequestAnalyseStore;
        const updateInfo = {tabKey};
        let {orgtype} = advanceSearchPara;

        if (tabKey == 2) {
            if (orgtype > 3) orgtype = 3;
        } else if (tabKey == 3) {
            if (orgtype > 2) orgtype = 2;
        }
        updateInfo['orgtype'] = orgtype;
        //条件初始化
        updateInfo.datarange = 1;
        updateInfo.dataid = '';

        updateInfo.dateType = 1;
        updateInfo.startDate = '';
        updateInfo.endDate = '';
        //获取默认值
        const {config = []} = advanceSearchData;
        if (orgtype > 1 && config.length > 0) {
            const {selectLinkageDatas = {}} = config[0];
            updateInfo['orgid'] = ReportUtil.getDefaultId(orgtype, selectLinkageDatas);
        }
        updateAdvanceSearchPara && updateAdvanceSearchPara(updateInfo);
    }
}